<?php

/**
 * ContactForm class.
 * ContactForm is the data structure for keeping
 * contact form data. It is used by the 'contact' action of 'SiteController'.
 */
class CpanelAccountsSearchForm extends CFormModel
{
	public $id_account;
	public $id_group;
	public $id_host;
	
	public $conditionVal;
	public $conditionField;
	public $searchRefresh;

	/**
	 * Declares the validation rules.
	 */
	public function rules()
	{
		return array(
			array('id_account, id_group, id_host, conditionVal, conditionField, searchRefresh', 'safe'),
		);
	}

	/**
	 * Declares customized attribute labels.
	 * If not declared here, an attribute would have a label that is
	 * the same as its name with the first letter in upper case.
	 */
	public function attributeLabels()
	{
		return array(

		);
	}
	
	public function getFieldLabel($field)
	{
		$labels = $this->attributeLabels();
		
		return empty($labels[$field])? '' : $labels[$field];
	}
	
	public function listAccounts()
	{
		return CHtml::listData(WhmAccount::model()->findAllByPk(User::model()->getFinalAccessdAccountIds()), 'id_account', 'domain');
	}

	public function listHosts()
	{
		return CHtml::listData(Host::model()->findAll(), 'id_host', 'name');
	}
		
	public function listGroups()
	{
		return CHtml::listData(Group::model()->findAllByPk(User::model()->getFinalAccessdGroupIds()), 'id_group', 'name');
	}
	
	public function listConditionFields()
	{
		return array(
			'domain' => 'Domain',
			'IP' => 'IP',
			'username' => 'Username',
			'package' => 'Package',
			'theme' => 'Theme'
		);
	}
	
	public function search()
	{
		$accessWhmaccountsId = User::model()->getFinalAccessdAccountIds();
		$accessWhmaccountsId = array_unique($accessWhmaccountsId);
		
		$whmaccountsId = array();
		if(!empty($this->id_account)){
			$whmaccountsId[] = $this->id_account;
		}else if(!empty($this->id_group)){
			$group = Group::model()->findByPk($this->id_group);
			if(!empty($group->WhmAccount)){
				foreach($group->WhmAccount as $ac){
					$whmaccountsId[] = $ac->id_account;
				}
			}
		}else if(!empty($this->id_host)){
			$host = Host::model()->findByPk($this->id_host);
			if(!empty($host->WhmAccount)){
				foreach($host->WhmAccount as $ac){
					$whmaccountsId[] = $ac->id_account;
				}
			}
		}else{
			$whmaccountsId = $accessWhmaccountsId;
		}
		$whmaccountsId = array_unique($whmaccountsId);
		
		
		$searchWhmaccountsId = array_intersect($accessWhmaccountsId, $whmaccountsId);
		
		$cpanelAccounts = null;
		if(!empty($searchWhmaccountsId)){
			if($this->searchRefresh == 1){
				foreach($searchWhmaccountsId as $id_account){
					$whmaccount = WhmAccount::model()->findByPk($id_account);
					$whmaccount->reLoadCpanelAccounts();
    				$whmaccount->reLoadFunctions();
				}
			}
			
			$criteria = new CDbCriteria();
			$criteria->addInCondition('id_account', $searchWhmaccountsId);
			if(!empty($this->conditionVal)){
				$criteria->addSearchCondition($this->conditionField, $this->conditionVal);
			}
			$cpanelAccounts = CpanelAccounts::model()->findAll($criteria);
			
		}
		return $cpanelAccounts;
	}
	
}